package pcb.views.sharedAssets 
{	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import pcb.views.sharedAssets.menu.TreeRoot;
	import pcb.views.sharedAssets.menu.TreeNode;
	import pcb.views.uievents.UIEvent;
	import pcb.views.uievents.TreeNodeEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class MenuAssetMain extends MovieClip
	{
		//private var context:LoaderContext;
		private var tree:TreeRoot;
		private var _enabled:Boolean;
		
		public function MenuAssetMain() 
		{
			//context = new LoaderContext();  
			//context.applicationDomain = ApplicationDomain.currentDomain;  
			
			//mcNext = this.getChildByName("mcNext") as MovieClip;
			//mcPrev = this.getChildByName("mcPrev") as MovieClip;
			
			//mcPrev.addEventListener(MouseEvent.CLICK, prevHandler);
			//mcNext.addEventListener(MouseEvent.CLICK, nextHandler);
			
			//loadMenuXml();
		}
		
		private function onClicked(e:TreeNodeEvent):void 
		{
			//trace("clicked in asset " + e.selectedNode.id);
			if(e.selectedNode.id != null){
				var ev:UIEvent = new UIEvent(UIEvent.MENU_ITEM_CLICKED, false);
				ev.id = e.selectedNode.id;
				dispatchEvent(ev);
			}else {
				var evNoItem:UIEvent = new UIEvent(UIEvent.MENU_NO_ITEM_CLICKED, false);
				evNoItem.data = getAncestryChain(e.selectedNode);		
				dispatchEvent(evNoItem);
			}
		}
		
		private function getAncestryChain(e:TreeNode):String
		{
			var out:String = e.label;
			
			if (e.parent is TreeNode) {				
				out += "," + e.parent.label;
			}
			
			return out;
		}
		
		
		
		
		/*
		private function nextHandler(e:MouseEvent):void 
		{
			tree.selectNextNode();
		}
		
		private function prevHandler(e:MouseEvent):void 
		{
			tree.selectPrevNode();
		}
		*/
		public function next():void 
		{
			tree.selectNextNode();
		}
		
		public function previous():void 
		{
			tree.selectPrevNode();
		}
		
		public function addDataProvider(data:XML):void 
		{
			tree = new TreeRoot(data, this);
			tree.addEventListener(TreeNodeEvent.CLICKED, onClicked);
			tree.selectNextNode();
		}
		
		public function set isEnabled(value:Boolean):void { 
			tree.enabled = value;
		}
		
		
		/*
		private function loadMenuXml():void
		{
			
			var xml_loader:URLLoader = new URLLoader();
			xml_loader.addEventListener(Event.COMPLETE, xml_loader_onComplete);
			xml_loader.addEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			xml_loader.load(new URLRequest("menu.xml"));
		}
		
		private function loader_onIOError(e:IOErrorEvent):void 
		{
			trace("Cmd_loadMenu > loader_onIOError");
			e.target.removeEventListener(Event.COMPLETE, xml_loader_onComplete);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
		}
		
		private function xml_loader_onComplete(e:Event):void 
		{
			var loader:URLLoader = URLLoader(e.target);
			
			var menuXml:XML = new XML(loader.data)
			
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			e.target.removeEventListener(Event.COMPLETE, xml_loader_onComplete);
			
			createmenu(menuXml);
			
		}
		*/
		
		/*
		private function createmenu(data:XML):void
		{			
			tree = new TreeRoot(data, this);
			
			tree.selectNextNode();
		}
		*/
		
	}
	
}